<?php 

class carModel extends appModel
{
	
    /*
     * @author zzx
     * @desc 获取中奖信息 
     */
    /* public function getPrize($openid)
    {
        $prize = array();
        $id = 0;
        $user_id = 0;
        $result = array();
        // 中奖逻辑
        $sql = "SELECT * FROM ".$this->table('chezhan_prize').' ORDER BY pri';
        $res = $this->getAll($sql);
        if($res){
            foreach($res as $k =>$v){
                
                if(rand(1,100) < $v['pby']){
                    $id = $v['id'];
                    break;
                } 
            }
        }
        
       
        
        if($id!=0){
            $sql2 = "SELECT id FROM ".$this->table('chezhan_prize_user')." WHERE pid='".$id."' and openid='".$openid."'";
            $res2 = $this->getRow($sql2);
            if($res2 && $res2['is_card']==1){
                $id = 0;
            }else{
                $sql3 = "SELECT * FROM ".$this->table('chezhan_prize_period').' WHERE begintime<'.time().' and endtime>'.time().' and nums>prizenums and pid='.$id;
                $result = $this->getRow($sql3);
                
                if($result){
                    $pnum = $result['prizenums']+1;
                    $sql4  = "UPDATE ".$this->table('chezhan_prize_period').' set prizenums ='.$pnum.' where id='.$result['id'];
                    $res4 = $this->query($sql4);
                    if(!$res2){
                        $sql5 = 'INSERT INTO '.$this->table('chezhan_prize_user').'(pid,ppid,openid,addtime) VALUES ("'.$id.'","'.$result['id'].'","'.$openid.'","'.time().'")';
                        $res3 = $this->query($sql5);
                        $user_id = $this->insert_id();
                    }else{
                        $user_id = $res2['id'];
                    }
                    
                }else{
                    $id = 0;
                }
            }
            
        }
        
        
        
        //获取中奖的记录
        if($id != 0){
            $sql6 = 'SELECT * FROM '.$this->table('chezhan_prize').' WHERE id="'.$id.'"';
            $prize = $this->getRow($sql6);
            
        }else{
            $prize['id']=0;
        }
        $prize['user_id'] = $user_id;
        
        return $prize;
    } */
    
    public function getPrize($openid,$card_type)
    {
        $prize = array();
        $id = 0;                    //奖项id
        $prize['user_id'] = 0;      // chenzhan_prize_user 用户中奖记录id
        $where = " WHERE 1=1 ";
        $result = "";
      
        //最近一条中奖记录
        $sql_1 = 'SELECT p2.mark,p1.addtime,pid,ppid,p1.id,p1.is_card,p2.prize_name FROM '.$this->table('chezhan_prize_user')." as p1, ".
                    $this->table('chezhan_prize').' as p2 '.
                " WHERE openid='".$openid."' and p1.pid = p2.id and p2.card_type = '".$card_type."'".
                " ORDER BY addtime desc ";
        $res = $this->getRow($sql_1);
        
       
        if(!$res){
            $res_1 = 0;
        }
        else
        {
            //最近一条中奖记录
            $res_1 = $res['addtime'];
            $id = $res['pid'];
            
            $prize['prize_name'] = $res['prize_name'];
            $prize['mark'] = $res['mark'];
            $prize['user_id'] = $res['id'];
            $prize['is_card'] = $res['is_card'];
            
        }
        
        $res_1 = date("Y-m-d",$res_1);
        $new = date("Y-m-d",time());
        
        
        //一天只能中一次奖
        if($res_1 != $new){
            //当天没有中过奖
        
            
            //在时间段内可抽取的奖项
            $sql3 = 'SELECT p1.pid,p1.id,p1.prizenums,p1.nums,p2.pby FROM '.$this->table('chezhan_prize_period').' as p1 ,'.
                     $this->table('chezhan_prize').' as p2 '.
                 ' WHERE begintime<'.time().' and endtime>'.time().' and p1.nums>p1.prizenums and p1.pid = p2.id and p2.card_type = '.$card_type.' order by p2.pri desc  '  ;
            
            $result3 = $this->getAll($sql3);
            
            if($result3){
                
                foreach($result3 as $k =>$v){
        
                    if(rand(1,100) < $v['pby']){
                        $id = $v['pid'];            //奖项id
                        $period_id = $v['id'];      //奖项时间段id
                        $temp_k = $v;
                        break;
                    }
                }
//                 }

                //中奖
                if($id!=0){
                    
                    $prize['prize_name'] = $temp_k['prize_name'];
                    $prize['mark'] = $temp_k['mark'];
                   
                    $sql2 = 'SELECT id,is_card FROM '.$this->table('chezhan_prize_user').' WHERE pid="'.$id.'" and openid="'.$openid.'"';
                    $res2 = $this->getRow($sql2);
                    
                    if(!$res2)
                    {
//                      //新中奖记录
                        
                        $this->setInc("prizenums", 1, "chezhan_prize", $id);
                        
                        $this->setInc("prizenums", 1, "chezhan_prize_period", $period_id);
                       
                         
                        $info['wxid'] = $_SESSION['wxid'];
                        $info['pid'] = $id;
                        $info['ppid'] = $period_id;
                        $info['addtime'] = time();
                        $info['openid'] = $openid;
                        
                        $user_id = $this->insert($info, 'chezhan_prize_user');
                        
//                         $sql8 = 'UPDATE '.$this->table('chezhan_prize').' set prizenums =prizenums+1 where id="'.$id.'"';
//                         $res8 = $this->query($sql8);
                        
//                         $sql4  = 'UPDATE '.$this->table('chezhan_prize_period').' set prizenums =prizenums+1 where id="'.$period_id.'"';
//                         $res4 = $this->query($sql4);
//                         $sql5 = 'INSERT INTO '.$this->table('chezhan_prize_user').'(pid,ppid,openid,addtime,wxid) VALUES ("'.$id.'","'.$period_id.'","'.$openid.'","'.time().'","'.$wxid.'")';
//                         $res3 = $this->query($sql5);
                        
                        $prize['user_id'] = $user_id;
                        $prize['is_card'] = 0;
                        
            
                    }
                    
                    
                }
                
            }
        }
 
        
        return $prize;
    }
    
    public function getCard($prize_user)
    {
        $sql = " SELECT p2.card_id FROM ".$this->table("chezhan_prize_user")." AS p1 "
              ." LEFT JOIN ".$this->table("chezhan_prize")." AS p2 ON p1.pid = p2.id  "   
              ." WHERE p1.id ='".$prize_user."'";
        
        $back = $this->getOne($sql);
        
        
        
//      $back = "plHTpwmlhQqJ_6itfucIAlIcADaI";
        
        if($back)
        {
            
            $cards = explode(",",$back);
            
            if(is_array($cards))
            {
                foreach ($cards as $k => $v)
                {
                    
                    $card_list[]['cardId'] = $v;
                }
            }
            else
            {
                
                $card_list[]['cardId'] = $back;
            }
            
            return $card_list;
        }
        else
        {
            return array();
        }
    }
    
	public function saveCard($id)
	{
	    
	    $this->setInc("is_card", 1, "chezhan_prize_user", $id);
	    
	}
	
	public function setInc($field,$step,$table,$id)
	{
	
	
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
	    return $this->query($sql);
	}
	
	/*
	 * @author zzx
	 * @desc 获取中奖信息记录
	 */
	public function getPrizeUserRecord($prize_user_id)
	{
        $prize_user = array();
        
        $sql = " SELECT p1.id,p2.support,p2.prize_name,p2.money,p1.is_card FROM ".$this->table("chezhan_prize_user")." AS p1 "
            ." LEFT JOIN ".$this->table("chezhan_prize")." AS p2 ON p1.pid = p2.id  "
                ." WHERE p1.id ='".$prize_user_id."'";
        
        $prize_user = $this->getRow($sql);
        
//         var_dump($prize_user);
        
        return $prize_user;
	}
	
	public function getRecord($id)
	{
	    if(!$id)
	    {
	        return false;
	    }
	    $sql = "SELECT *,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
	    $result=$this->getRow($sql);
	
	    if($result)
	    {
	
	        return $result;
	    }
	    else
	    {
	        return false;
	    }
	}
	
}

?>
